package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.UserInfo;
import com.itheima.mapper.UserInfoMapper;
import com.itheima.service.db.UserInfoService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public PageBeanVo findByPage(Integer pageNum, Integer pageSize) {
        //1.创建分页对象,设置分页参数
        Page<UserInfo> page = new Page<>(pageNum, pageSize);

        //2.若有其他条件的话创建条件对象
        //3.调用mapper的selectPage方法
        userInfoMapper.selectPage(page,null);

        //4.封装pageBeanVo且返回
        return new PageBeanVo(pageNum,pageSize,page.getTotal(), page.getRecords());
    }
    //根据id集合查询
    @Override
    public List<UserInfo> findByUserIds(List<Long> blackUserIds) {
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.in(UserInfo::getId)
        return userInfoMapper.selectBatchIds(blackUserIds);
    }

    @Override
    public Page<UserInfo> findByPageAndIds(Integer pageNum, Integer pageSize, String nickname, List<Long> ids) {
        Page<UserInfo> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<UserInfo> lqw=new LambdaQueryWrapper<>();
        lqw.in(ids!=null,UserInfo::getId,ids)
                .eq(nickname!=null,UserInfo::getNickname,nickname);

        return  userInfoMapper.selectPage(page, lqw);

    }
}
